<template>
    <div class="network-page">
        <!-- 顶部导航栏 -->
        <HeaderNav active-menu="network" />

        <!-- 主内容区域 -->
        <div class="main-content">
            <!-- 左侧边栏 -->
            <Sidebar :active-sidebar="activeSidebar" @sidebar-select="handleSidebarSelect" />

            <!-- 右侧内容区域 -->
            <main class="network-main">
                <div class="network-container">
                    <!-- 左侧：用户列表 -->
                    <div class="users-section">
                        <!-- 原有用户列表内容保持不变 -->
                        <div class="section-header">
                            <h2 class="section-title">发现用户</h2>
                            <div class="search-box">
                                <el-input v-model="searchKeyword" placeholder="搜索用户..." prefix-icon="Search" clearable
                                    @input="handleSearch" />
                            </div>
                        </div>

                        <div class="users-list">
                            <div v-if="loading && users.length === 0" class="loading-state">
                                <el-icon class="is-loading">
                                    <Loading />
                                </el-icon>
                                <span>加载中...</span>
                            </div>

                            <template v-else>
                                <UserCard v-for="user in users" :key="user.user_id" :user="user" />

                                <div v-if="users.length === 0" class="empty-state">
                                    <el-empty description="暂无用户" />
                                </div>

                                <!-- 加载更多 -->
                                <div v-if="hasMore" class="load-more">
                                    <el-button :loading="loading" :disabled="loading" @click="loadMore" text>
                                        {{ loading ? '加载中...' : '加载更多' }}
                                    </el-button>
                                </div>

                                <div v-else-if="users.length > 0" class="no-more">
                                    没有更多用户了
                                </div>
                            </template>
                        </div>
                    </div>

                    <!-- 右侧：用户统计 -->
                    <div class="stats-section">
                        <UserStatsCard />
                    </div>
                </div>
            </main>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Loading, Search } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import HeaderNav from '@/components/layout/HeaderNav.vue'
import UserCard from '@/components/network/UserCard.vue'
import UserStatsCard from '@/components/network/UserStatsCard.vue'
import Sidebar from '@/components/layout/Sidebar.vue'

const userStore = useUserStore()
const router = useRouter()

const users = ref([])
const loading = ref(false)
const hasMore = ref(true)
const page = ref(1)
const pageSize = ref(10)
const searchKeyword = ref('')
const searchTimer = ref(null)
const activeSidebar = ref('network')

// 侧边栏导航处理
const handleSidebarSelect = (index) => {
    activeSidebar.value = index
    switch (index) {
        case 'dashboard':
            router.push('/')
            break
        case 'social':
            router.push('/social')
            break
        case 'circles':
            router.push('/circle')
            break
        case 'chat':
            router.push('/chat')
            break
        case 'lost-found':
            router.push('/lost-and-found')
            break
        case 'second-hand':
            router.push('/second-hand')
            break
        case 'recruitment':
            router.push('/recruitment')
            break
        default:
            ElMessage.success(`当前已是在 ${index}页面`)
    }
}

// 加载用户列表
const loadUsers = async (reset = false) => {
    if (loading.value) return

    try {
        loading.value = true

        if (reset) {
            page.value = 1
            users.value = []
        }

        const params = {
            page: page.value,
            pageSize: pageSize.value
        }

        if (searchKeyword.value.trim()) {
            params.keyword = searchKeyword.value.trim()
        }

        const response = await userStore.getUserListAction(params)
        if (response.code === 0) {
            const userList = response.data.list || []

            if (reset) {
                users.value = userList
            } else {
                users.value = [...users.value, ...userList]
            }

            // 检查是否还有更多数据
            hasMore.value = userList.length === pageSize.value
        }
    } catch (error) {
        console.error('加载用户列表失败:', error)
        ElMessage.error('加载用户列表失败')
    } finally {
        loading.value = false
    }
}

// 加载更多
const loadMore = () => {
    page.value += 1
    loadUsers(false)
}

// 搜索处理
const handleSearch = () => {
    clearTimeout(searchTimer.value)
    searchTimer.value = setTimeout(() => {
        loadUsers(true)
    }, 500)
}

onMounted(() => {
    loadUsers(true)
})
</script>

<style scoped>
.network-page {
    min-height: 100vh;
    background-color: #f5f7fa;
    display: flex;
    flex-direction: column;
}

/* 主内容区域 */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.network-main {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    margin-left: 280px;
}

.network-container {
    display: grid;
    grid-template-columns: 1fr 300px;
    gap: 24px;
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.users-section {
    background: white;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
}

.section-title {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    color: #333;
}

.search-box {
    width: 300px;
}

.users-list {
    min-height: 400px;
}

.loading-state {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px;
    color: #999;
    gap: 8px;
}

.empty-state {
    padding: 40px 0;
}

.load-more {
    text-align: center;
    padding: 20px;
}

.no-more {
    text-align: center;
    padding: 20px;
    color: #999;
    font-size: 14px;
}

.stats-section {
    position: sticky;
    height: fit-content;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .network-container {
        grid-template-columns: 1fr;
        padding: 60px 16px 16px;
    }

    .section-header {
        flex-direction: column;
        gap: 16px;
        align-items: stretch;
    }

    .search-box {
        width: 100%;
    }

    .stats-section {
        position: static;
    }
}

@media (max-width: 768px) {
    .network-main {
        margin-left: 240px;
    }

    .network-container {
        grid-template-columns: 1fr;
        padding: 16px;
    }
}
</style>